#pragma once

#include "base/core/singleton.h"
#include "Common/MallDefine.h"
#include "Common/MessageDispatch/IMessageDispatch.h"
#include "ProtoBuffer/AllProtocol.h"
#include "Common/TableData/MallMallCfg.h"
#include "Common/TableData/MallMalltypeCfg.h"
#include "Common/TableData/MailMailCfg.h"
#include "Common/CharacterDefine.h"

class Player;

class GMallManager : public IClientMessageHandler, public IServerMessageHandler, public ManualSingleton < GMallManager >
{
public:
	bool 				Init();
	void 				UnInit();

	/////////  IClientMessageHandler ///////
	virtual void		onClientMessage(uint32_t actionId, RECV_PACKAGE& package, uint64_t Id = 0);
	virtual const char* getClientHandlerDebugInfo(void) { return "GMallManager::onClientMessage"; }

	/////////  IServerMessageHandler ///////
	virtual void		onServerMessage(uint32_t actionId, RECV_PACKAGE &package);
	virtual const char* getServerHandlerDebugInfo(void){ return "GMallManager::onServerMessage"; }

public:
	//购买
	void Buy(RECV_PACKAGE &package, CharIDType Id);

	//出售
	void Sell(RECV_PACKAGE &package, CharIDType Id);

	//兑换
	void Exchange(RECV_PACKAGE &package, CharIDType Id);

	//赠送
	void Handsel(RECV_PACKAGE &package, CharIDType Id);

	//检查等级 vip等
	bool CheckPlayer(Player* pPlayer, const MallMallCfgInfo* pMallCfgInfo);

	//检查钱币
	bool CheckMoney(Player* pPlayer, const MallMallCfgInfo* pMallCfgInfo, uint32_t count, bool isHandsel = false);

	//检查商品性质
	bool CheckGoods(Player* pPlayer, const MallMallCfgInfo* pMallCfgInfo, uint32_t count);

	//检查跑商
	bool CheckRunBus_buy(Player* pPlayer, const MallMallCfgInfo* pMallCfgInfo, uint32_t &fund);

	//检查跑商
	bool CheckRunBus_sell(Player* pPlayer, const MallMallCfgInfo* pMallCfgInfo, Mall_Sell_VerifyReq &req/*, uint32_t count*/);

	//检查背包
	bool CheckPackage(Player* pPlayer, const MallMallCfgInfo* pMallCfgInfo, uint32_t count);

	//扣除钱币
	int64_t CostMoney(Player* pPlayer, const MallMallCfgInfo* pMallCfgInfo, uint32_t count, bool isHandsel = false);

	//发放物品
	void AddItem(Player* pPlayer, const MallMallCfgInfo* pMallCfgInfo, uint32_t count, int32_t money = 0);

	//中心服验证返回
	void DoBuyVerifyRsp(RECV_PACKAGE &package);

	//出售中心服验证返回
	void DoSellVerifyRsp(RECV_PACKAGE &package);

	//赠送中心服验证返回
	void DoHandselVerifyRsp(RECV_PACKAGE &package);

	/*
	itemType:商品类型 1:道具 2:装备 3:错误类型
	itemId:购买的物品ID
	count:购买的物品数量
	disCount:是否打折
	price:购买价格
	std::map<Mall_MoneyType, uint32_t> <货币类型,花费>
	*/
	void CountToBuy(uint32_t itemType, uint32_t itemId, uint32_t count, bool disCount, uint32_t price, std::map<Mall_MoneyType, uint32_t> money_cost_map);

public:
	GMallManager();
	virtual ~GMallManager();
};

#define g_MakeGMallMgr() (GMallManager::Instance())
#define g_GetGMallMgr()  (GMallManager::GetInstance())
#define g_DelGMallMgr()  (GMallManager::Destroy())